import java.util.List;
import java.util.ArrayList;

// 组件接口
interface Component {
    void display();
}

// 叶子节点：专业
class Major implements Component {
    private String name;

    public Major(String name) {
        this.name = name;
    }

    @Override
    public void display() {
        System.out.println(name);
    }
}

// 叶子节点：行政部门
class AdministrativeDepartment implements Component {
    private String name;

    public AdministrativeDepartment(String name) {
        this.name = name;
    }

    @Override
    public void display() {
        System.out.println(name);
    }

    // 获取部门名称
    public String getName() {
        return name;
    }
}

// 组合节点：部门（可以包含专业）
class Department implements Component {
    private String name;
    private List<Component> components = new ArrayList<>();

    public Department(String name) {
        this.name = name;
    }

    // 添加子组件
    public void add(Component component) {
        components.add(component);
    }

    // 移除子组件
    public void remove(Component component) {
        components.remove(component);
    }

    @Override
    public void display() {
        // 先打印该部门下的所有专业
        for (Component component : components) {
            component.display(); // 展示每个子组件（专业）
        }
    }

    // 获取所有的专业
    public List<Component> getMajors() {
        List<Component> majors = new ArrayList<>();
        for (Component component : components) {
            if (component instanceof Major) {
                majors.add(component);
            }
        }
        return majors;
    }
}

// 组合节点：学校（包含多个部门和行政单位）
class School implements Component {
    private String name;
    private List<Component> components = new ArrayList<>();

    public School(String name) {
        this.name = name;
    }

    // 添加子组件
    public void add(Component component) {
        components.add(component);
    }

    // 移除子组件
    public void remove(Component component) {
        components.remove(component);
    }

    @Override
    public void display() {
        // 打印学校名称
        System.out.println("----------------" + name + "的专业和行政单位如下:");

        // 先输出所有专业，确保顺序：计算机科学专业、通信工程专业、软件工程专业、人工智能专业
        for (Component component : components) {
            if (component instanceof Department) {
                Department department = (Department) component;
                // 输出该部门下的所有专业
                department.display();
            }
        }

        // 输出行政单位，确保人事处在教务处之前
        for (Component component : components) {
            if (component instanceof AdministrativeDepartment) {
                // 确保人事处在教务处之前打印
                if ("人事处".equals(((AdministrativeDepartment) component).getName())) {
                    component.display();  // 展示人事处
                }
            }
        }

        // 输出教务处
        for (Component component : components) {
            if (component instanceof AdministrativeDepartment) {
                // 确保教务处排在人事处之后
                if ("教务处".equals(((AdministrativeDepartment) component).getName())) {
                    component.display();  // 展示教务处
                }
            }
        }
    }
}

// 客户端：创建学校的组织结构并显示
public class UniversityStructure {
    public static void main(String[] args) {
        // 创建专业
        Major csMajor = new Major("计算机科学专业");
        Major ceMajor = new Major("通信工程专业");
        Major seMajor = new Major("软件工程专业");
        Major aiMajor = new Major("人工智能专业");

        // 创建行政部门
        AdministrativeDepartment academicAffairs = new AdministrativeDepartment("教务处");
        AdministrativeDepartment hrDepartment = new AdministrativeDepartment("人事处");

        // 创建教学部门并添加专业
        Department aiCollege = new Department("人工智能学院");
        aiCollege.add(seMajor);
        aiCollege.add(aiMajor);

        Department infoCollege = new Department("信息学院");
        infoCollege.add(csMajor);
        infoCollege.add(ceMajor);

        // 创建学校并添加教学部门和行政部门
        School school = new School("河南工业大学");
        school.add(aiCollege);
        school.add(infoCollege);
        school.add(academicAffairs);
        school.add(hrDepartment);

        // 显示学校的专业和行政单位
        school.display();
    }
}
